import Vue from "vue";
import VueRouter from "vue-router";


Vue.use(VueRouter);
import Index from "@/views/index/index.vue";

// 一级路由
const Login = () => import("@/views/login/login.vue");
const Layout = () => import("@/views/layout/Layout.vue");


// 项目配置的初始模板
const routes = [
  { path: "/", redirect: "/Login" },
  { path: "/Login", component: Login },
  {
    path: "/layout",
    component: Layout,
    children: [
      {
        path: "/index",
        component:Index
      },
    ],
  },
];

//添加以下代码 重写路由push方法
const originalPush = VueRouter.prototype.push;
VueRouter.prototype.push = function push(location) {
  return originalPush.call(this, location).catch((err) => err);
};
// 路由前置守卫 -- 守卫路由

const router = new VueRouter({
  routes,
});
router.beforeEach((to, from, next) => {
  // 出口
  // 白手套 白名单 [xxx, xxx].includes
  if (to.path === "/login") {
    next();
  } else {
    const token = localStorage.getItem("token");
    // 如果你有token 就代表你登录了
    if (token) {
      next();
    
    } else {
      next({ path: "/login" });
    }
  }
});

export default router;
